package com.basketball.gym.controller;


import com.basketball.gym.pojo.Information;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.User;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.InformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-21
 */
@Api(tags = "信息模块")
@RestController
@RequestMapping("/gym/information")
public class InformationController {
    @Autowired
    private InformationService informationService;

    @ApiOperation("完善信息")
    @PreAuthorize("hasRole('user')")
    @PostMapping
    public ResponseData<Information> insertInformation(@Validated(Information.insert.class) @RequestBody Information information, @ApiIgnore @ModelAttribute("activeUser") User user) {
        try {
            return informationService.insertInformation(information, user);
        } catch (DuplicateKeyException e) {
            return new ResponseData<>(ResponseState.OPERATION_HAI_FINISH.getMessage(), ResponseState.OPERATION_HAI_FINISH.getValue());
        }
    }

    @ApiOperation("更新信息")
    @PreAuthorize("hasRole('user')")
    @PutMapping
    public ResponseData<Information> updateInformation(@Validated(Information.update.class) @RequestBody Information information, @ApiIgnore @ModelAttribute("activeUser") User user) {
        return informationService.updateInformation(information, user);
    }

    @ApiOperation("查询信息")
    @PreAuthorize("hasRole('user')")
    @GetMapping
    public ResponseData<Information> getInformation(@ApiIgnore @ModelAttribute("acticeUser") User user) {
        return informationService.getInformation(user);
    }


}

